package com.example.yls.demoa;

import android.animation.AnimatorInflater;
import android.animation.ValueAnimator;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
private Button btn1;
private ImageView img1;
private Button btn2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        btn1Event();
        btn2Event();
    }

    private void btn2Event() {

       btn2.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               //startAnimationSetWithJava1();
               startAnimationSetWithXML1();
           }
       });

    }

    private void startAnimationSetWithXML1() {
      ValueAnimator valueAnimator= (ValueAnimator) AnimatorInflater.loadAnimator(MainActivity.this
      ,R.animator.set);
      valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
          @Override
          public void onAnimationUpdate(ValueAnimator valueAnimator) {
              int value= (int) valueAnimator.getAnimatedValue();
              btn2.getLayoutParams().width=value;
              btn2.requestLayout();
              btn2.getLayoutParams().height=value;
              btn2.setBackgroundColor(Color.argb(255,value/2,0,0));

          }

      });
        valueAnimator.start();


    }



    private void startAnimationSetWithJava1() {
        ValueAnimator valueAnimator=ValueAnimator.ofInt(btn2.getLayoutParams().width,
                500);
        valueAnimator.setDuration(5000);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                int value= (int) valueAnimator.getAnimatedValue();
                btn2.getLayoutParams().width=value;
                btn2.requestLayout();
            }
        });
        valueAnimator.start();
    }

    private void btn1Event() {
       btn1.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               //RotateAnimationEvent();
               //startAnimationSetWithJava();
               startAnimationSetWithXML();
           }
       });

    }

    private void startAnimationSetWithXML() {

           AnimationSet animationSet= (AnimationSet) AnimationUtils.loadAnimation
                   (MainActivity.this,
                           R.anim.set
           );
           img1.startAnimation(animationSet);
    }

    private void startAnimationSetWithJava() {
        AnimationSet animationSet=new AnimationSet(true);
        TranslateAnimation translateAnimation=new TranslateAnimation(0,300,0,300);
        ScaleAnimation scaleAnimation=new ScaleAnimation(1,3,1,3);
  RotateAnimation rotateAnimation=
               new RotateAnimation(0,360,180,180);
       animationSet.addAnimation(translateAnimation);
       animationSet.addAnimation(scaleAnimation);
       //animationSet.addAnimation(rotateAnimation);
        animationSet.setDuration(3000);
        animationSet.setRepeatCount(2);
        animationSet.setRepeatMode(Animation.REVERSE);

       img1.startAnimation(animationSet);
    }
    private void RotateAnimationEvent() {
      /* RotateAnimation rotateAnimation=
               new RotateAnimation(0,360,180,180);
       rotateAnimation.setDuration(3000);
       rotateAnimation.setRepeatCount(2);
       rotateAnimation.setRepeatMode(Animation.REVERSE);
       img1.startAnimation(rotateAnimation);*/

       RotateAnimation rotateAnimation1= (RotateAnimation) AnimationUtils.loadAnimation
               (MainActivity.this,R.anim.rotate);
       img1.startAnimation(rotateAnimation1);
    }
    private void initView() {
           btn1=findViewById(R.id.btn1);
           img1=findViewById(R.id.img1);
           btn2=findViewById(R.id.btn2);
    }
}
